package org.hackystat.devcathlon.ui.page.scoreboard.eventspanel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.event.EventScore;
import org.hackystat.devcathlon.engine.match.MatchManager;

/**
 * The Events element for the scoreboard.
 * 
 * @author Daniel Arakaki, John Zhou
 */
public class FrontEventsPanel extends Panel implements Serializable {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Initialize sessions and add a label to scoreboard.
   * 
   * @param wicketId A Wicket ID from the html file..
   * @param matchNames A list of matche names.
   */
  public FrontEventsPanel(String wicketId, List<String> matchNames) {
    super(wicketId);
    
    if (matchNames.isEmpty()) {
      add(new Label("matchNameHeader", ""));
      
      List<EventScore> scoresList = new ArrayList<EventScore>();

      DataView scoreView = new DataView("scoreList", new ListDataProvider(scoresList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          item.add(new Label("teamName", ""));
          item.add(new Label("points", ""));
          item.add(new Label("explantion", ""));
        }

      };
      add(scoreView);
   
    }
    else {
      add(new Label("matchNameHeader", matchNames.get(0)));
      
      List<EventScore> scoresList = new ArrayList<EventScore>();

      for (EventScore s : MatchManager.getInstance().getMatch(matchNames.get(0)).getEventScores()) {
        scoresList.add(s);
      }

      DataView scoreView = new DataView("scoreList", new ListDataProvider(scoresList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          EventScore scoreItem = (EventScore) item.getModelObject();
          
          try {
            item.add(new Label("date", scoreItem.getTimestampDate().toString()));
          }
          catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          item.add(new Label("teamName", scoreItem.getTeam().getName()));
          item.add(new Label("points", String.valueOf(scoreItem.getBaseScore())));
          item.add(new Label("explanation", scoreItem.getExplanation()));
        }

      };
      //scoreView.setItemsPerPage(3);
      //add(new PagingNavigator("navigator", scoreView));
      add(scoreView); 
    }
  }
}
